﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Ornithopter.FlashDiskHelper
{
	/// <summary>
	/// SpaceIndicator.xaml 的交互逻辑
	/// </summary>
	public partial class SpaceIndicator : UserControl
	{
        public double Max
        {
            get { return (double)GetValue(MaxProperty); }
            set { SetValue(MaxProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Max.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaxProperty =
            DependencyProperty.Register("Max", typeof(double), typeof(SpaceIndicator), new UIPropertyMetadata(100.0));

        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(SpaceIndicator), new UIPropertyMetadata(50.0, ValueChanged));


        private static void ValueChanged(DependencyObject o,DependencyPropertyChangedEventArgs e)
        {
            ((SpaceIndicator)o).Show();
        }
		public SpaceIndicator()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(SpaceIndicator_Loaded);
		}

        void SpaceIndicator_Loaded(object sender, RoutedEventArgs e)
        {
            Show();
        }

        public void Show()
        {
            double percent = Value / Max;
            string imagePath = "pack://application:,,,/Images/";
            if (percent <= 0.5)
            {
                imagePath += "Track_FG_Green.png";
            }
            else if (percent <= 0.7)
            {
                imagePath = "Track_FG_Orange.png";
            }
            else
            {
                imagePath = "Track_FG_Red.png";
            }
            brush.ImageSource = new BitmapImage(new Uri(imagePath, UriKind.Absolute));
            indicator.Width = Math.Round(percent * this.Width);
        }
	}
}